package org.grottarossa.ui.core.controls.searchuser;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.users.User;
import org.grottarossa.template.data.pojo.users.UserList;
import org.grottarossa.ui.core.ApplicationSession;


/***
 * Gestisce la logica del controllo di ricerca e selezione utente/ruolo
 * @author 
 *
 */
public class SearchUserPresenter
{
	private final ResourceBundle m_localizer;

	/*** View ***/
	private final SearchUser m_view;

	/*** Stato della maschera di filtraggio ***/
	private SearchUserData m_filter;

	/*** Ultima selezione di filtraggio ***/
	private SearchUserData m_lastSearch;

	/*** Lista degli utenti ***/
	private UserList m_users;

	/*** Lista dei ruoli ***/
	private ArrayList<String> m_roles;

	/*** Elemento selezionato ***/
	private String m_selected;

	/*** Listeners ***/
	private ArrayList<ISearchUserListener> m_listeners = new ArrayList<ISearchUserListener>();

	private final ApplicationSession m_appSession;

	public SearchUserPresenter(ApplicationSession appSession, SearchUser view, ResourceBundle localizer)
	{
		this.m_appSession = appSession;
		m_view = view;
		m_localizer = localizer;
		m_view.setPresenter(this);
	}

	public void initialize()
	{
		m_filter = new SearchUserData();
		m_lastSearch = new SearchUserData();
		m_view.setSearchFilter(m_filter);

		try
		{
			m_users = m_appSession.getBuilder().getUserManagement().getUsers(null);
			m_roles = m_appSession.getBuilder().getUserManagement().getRoles();
		}
		catch(Exception ex)
		{
			m_users = new UserList();
			m_view.showError(ex);
		}
	}

	/***
	 * Ricerca gli utenti in base al filtro selezionato e popola l'elenco dei risultati
	 */
	public void search()
	{
		Hashtable<String, String> found = new Hashtable<String, String>();
		m_lastSearch = m_filter.clone();

		if(m_filter.getTypeFilter().equals(m_localizer.getString("form.types.users")))
		{
			boolean loginIdFilter = m_filter.getFieldFilter().equals(m_localizer.getString("form.fieldfilter.loginid"));
			boolean displayNameFilter = m_filter.getFieldFilter().equals(m_localizer.getString("form.fieldfilter.displayname"));
			boolean firstNameFilter = m_filter.getFieldFilter().equals(m_localizer.getString("form.fieldfilter.firstname"));
			boolean lastNameFilter = m_filter.getFieldFilter().equals(m_localizer.getString("form.fieldfilter.lastname"));

			if(m_users != null)
				for(User u : m_users)
				{
					if(loginIdFilter && u.getLoginID().toUpperCase().startsWith(m_filter.getTextFilter().toUpperCase()))
						found.put(u.getLoginID(), u.getDisplayName());
					else if(displayNameFilter && u.getDisplayName().toUpperCase().startsWith(m_filter.getTextFilter().toUpperCase()))
						found.put(u.getLoginID(), u.getDisplayName());
					else if(firstNameFilter && u.getFirstName().toUpperCase().startsWith(m_filter.getTextFilter().toUpperCase()))
						found.put(u.getLoginID(), u.getDisplayName());
					else if(lastNameFilter && u.getLastName().toUpperCase().startsWith(m_filter.getTextFilter().toUpperCase()))
						found.put(u.getLoginID(), u.getDisplayName());
				}
		}
		else
		{
			if(m_roles != null)
				for(String role : m_roles)
				{
					if(role.toUpperCase().startsWith(m_filter.getTextFilter().toUpperCase()))
						found.put(role, role);
				}
		}

		m_view.showResults(found);
	}

	/***
	 * Notifica ai listeners l'elemento selezionato
	 */
	public void ok()
	{
		m_selected = m_view.getSelectedUser();

		if(m_selected == null)
		{
			m_view.showWarning(m_localizer.getString("form.messages.selectvalue"));
			return;
		}

		boolean isUser = m_lastSearch.getTypeFilter().equals(m_localizer.getString("form.types.users"));
		User user = null;

		if(isUser)
		{
			for(User u : m_users)
			{
				if(u.getLoginID().equals(m_selected))
				{
					user = u;
					break;
				}
			}
		}

		for(ISearchUserListener l : m_listeners)
		{

			l.ok(new SearchUserEvent(isUser, m_selected, user));
		}
	}

	/***
	 * Notifica ai listsners l'annullamento
	 */
	public void cancel()
	{
		for(ISearchUserListener l : m_listeners)
		{
			l.cancel();
		}
	}

	/***
	 * Aggiunge un listener
	 * @param listener
	 */
	public void addListener(ISearchUserListener listener)
	{
		m_listeners.add(listener);
	}
}
